package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/17.
 * 修改昵称
 */

public class NicknameChangeActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.admin_name)
    EditText admin_name;

    @Bind(R.id.button)
    Button button;
Context context;
    private MyProgressDialog mDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nickname_change);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        button.setOnClickListener(this);
        MineFragmentModel model = UserInfoUtils.readUserInfo(this);
        //姓名
        if (!Sundry.getStinges(model.getNickname())){
            admin_name.setHint(model.getNickname());
        }else {
            admin_name.setHint(getResources().getString(R.string.name));
        }
        admin_name
                .addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start,
                                              int before, int count) {
                        String editable = admin_name
                                .getText().toString();

                        String str = Sundry.stringFilter(editable);

                        if (!editable.equals(str)) {
                            admin_name.setText(str);
                        }
                    }
                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1,
                                                  int arg2, int arg3) {
                    }
                    @Override
                    public void afterTextChanged(Editable arg0) {
                        // TODO Auto-generated method stub
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.retur:
                finish();
                break;
            case  R.id.button:
                if (TextUtils.isEmpty(admin_name.getText().toString().trim())){
                    ToastUtils.show(context, getResources().getString(R.string.please_nickname));
                    return;
                }


            processname();
                break;
        }
    }
    //昵称
    private void processname(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);

            HttpClient.editNickname(admin_name.getText().toString().trim(), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    if (mDialog!=null){
                        mDialog.dismiss();
                    }
                    System.out.println("stringmd5result"+result);
                    List<RegistrationModel> getnote = JsonTools.getnote(result);
                    if (getnote!=null||getnote.size()>0){
                        String status = getnote.get(0).getStatus();
                        String getMsg = getnote.get(0).getMsg();
                        if (status.equals("200")){
                            ToastUtils.show(context, getMsg);
                            finish();
                            UserInfoUtils.getInstans().updatename(context,admin_name.getText().toString().trim());
                        }else {
                            ToastUtils.show(context, getMsg);
                        }
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    if (mDialog!=null){
                        mDialog.dismiss();
                    }
                    System.out.println("stringmd5resultThrowable"+ex.getMessage());
                    ToastUtils.Toastshow(context, getResources().getString(R.string.server));
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    if (mDialog!=null){
                        mDialog.dismiss();
                    }
                    System.out.println("stringmd5resultCancelledException"+cex.getMessage());
                    ToastUtils.Toastshow(context, getResources().getString(R.string.server));
                }

                @Override
                public void onFinished() {

                }
            });
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
}

